using Microsoft.AspNetCore.Mvc;     // 因为ControllerBase等需要引入
using MyWebApi.Api.Entity;      // 因为Users表需要引入
using System.Collections.Generic;   // 因为泛型需要引入
using System.Linq;     // 因为Where 引入之后我们可以使用一种类似SQL的语法来查询任何形式的数据
using System;
using MyWebApi.Api.Repository;
using MyWebApi.Api.ParamModel;
using MyWebApi.Api.Utils;
using Microsoft.Extensions.Configuration;
using Microsoft.AspNetCore.Authorization;
using Microsoft.AspNetCore.Hosting;


// 
// 商城管理控制器
// 
namespace MyWebApi.Api.Controllers
{
    // 特性
    // [Authorize]  // [Authorize]下的所有Api接口如：查询、删除、创建用户不能匿名访问，需要Token令牌才可访问
    [ApiController]
    [Route("[controller]")]

    public class ShoppingMallController : ControllerBase
    {

        // 实例化数据库
        // private MyWebApi.Api.Db.MyWebApiDb db = new Db.MyWebApiDb();

        // 定义为IRepository<T>的泛型接口未知数 T 为 users
        private IRepository<users> _usersRespository;   // 现在_usersRespository里包含IRepository<T>泛型接口里面的属性(可以使用这些属性来进行对Users表的操作)
        private IRepository<classify> _classify;

        private IRepository<shopping_mall> _shopping_mall;
        private IRepository<payment> _payment;
        private IRepository<slideshow> _slideshow;

        // 文件上传需要
        private readonly IWebHostEnvironment _hostingEnvironment;


        private TokenParameter _tokenParameter;
        private readonly IConfiguration _configuration;

        // 一个构造函数：初始化UsersController控制器，使该控制器一开始就有以下的属性
        public ShoppingMallController(IConfiguration configuration,
         IRepository<users> usersRespository,
         IRepository<classify> classify,
         IRepository<shopping_mall> shopping_mall,
         IRepository<payment> payment,
         IRepository<slideshow> slideshow,
         IWebHostEnvironment hostingEnvironment
         )
        {
            // 文件上传需要
            _hostingEnvironment = hostingEnvironment;

            _configuration = configuration;
            _tokenParameter = _configuration.GetSection("tokenParameter").Get<TokenParameter>();

            _usersRespository = usersRespository;
            _classify = classify;
            _shopping_mall = shopping_mall;
            _payment = payment;
            _slideshow = slideshow;
        }


        // 添加商城信息(更新商品直接添加，查询的时候查创建时间倒序)
        [HttpPost]
        public dynamic addShoppingMall(ShoppingMallModel model)
        {
            var shoppingMallLogo = model.ShoppingMallLogo;
            var shoppingMallName = model.ShoppingMallName;
            var iCPPutOnRecords = model.ICPPutOnRecords;
            var publicPutOnRecords = model.PublicPutOnRecords;

            if (shoppingMallLogo != null && shoppingMallName != null)
            {

                var addShoppingMall = new shopping_mall
                {
                    shoppingMallLogo = shoppingMallLogo,
                    shoppingMallName = shoppingMallName,
                    ICPPutOnRecords = iCPPutOnRecords,
                    PublicPutOnRecords = publicPutOnRecords
                };
                _shopping_mall.Insert(addShoppingMall);

                var res = new
                {
                    Code = 1000,
                    Data = addShoppingMall,
                    Msg = "商城信息添加成功"
                };
                return JsonHelper.Serialize(res);

            }
            else
            {
                var res = new
                {
                    Code = 1001,
                    Data = "商城Logo和商城名称不能为空",
                    Msg = "商城Logo和商城名称不能为空"
                };
                return JsonHelper.Serialize(res);
            }


        }

        // 查询商城信息
        [HttpGet]
        public dynamic selShoppingMall()
        {
            var shoppingMall = _shopping_mall.Table.Where(x => x.IsDeleted == false).OrderByDescending(x => x.CreatedTime).ToList().FirstOrDefault();

            var res = new
            {
                Code = 1000,
                Data = shoppingMall,
                Msg = "查询商城信息成功"
            };
            return JsonHelper.Serialize(res);

        }

        // # ======================================================================================
        // #       支付方式
        // # ======================================================================================


        // 添加支付方式
        [HttpPost, Route("add_payment")]
        public dynamic addPayment()
        {
            // 获取前端FormData中的文件
            var files = HttpContext.Request.Form.Files;

            string webRootPath = _hostingEnvironment.WebRootPath;// 获取目录下的...Api/wwwroot路径 
            // 支付方式图片
            var pictureShowfilePath = FileSaveHelper.Filesave(files, webRootPath);  // 封装的文件处理方法

            // 获取前端FormData中的数据
            // 支付方式
            var patternOfPayment = HttpContext.Request.Form["patternOfPayment"];

            var addPayment = new payment
            {
                patternOfPayment = patternOfPayment,
                picture = pictureShowfilePath
            };
            _payment.Insert(addPayment);

            var res = new
            {
                Code = 1000,
                Data = addPayment,
                Msg = "添加支付方式成功"
            };
            return JsonHelper.Serialize(res);
        }

        // 查询支付方式
        [HttpGet, Route("sel_payment")]
        public dynamic selPayment()
        {
            var Payment = _payment.Table.Where(x => x.IsDeleted == false).OrderByDescending(x => x.CreatedTime).ToList().FirstOrDefault();

            var res = new
            {
                Code = 1000,
                Data = Payment,
                Msg = "查询支付方式成功"
            };
            return JsonHelper.Serialize(res);
        }


        // # ======================================================================================
        // #       轮播图
        // # ======================================================================================

        // 添加轮播图
        [HttpPost,Route("add_slideshow")]
        public dynamic AddSlideshowPicture()
        {
            // 获取需要存入数据库的值
            // 轮播图顺序
            // 1 第一张轮播图 ， 2 第二张轮播图 ， 3 第三张轮播图。。。。。。
            var sequence = HttpContext.Request.Form["sequence"];

            // 获取前轮播图
            // 获取前端FormData中的文件
            var byThePicture = HttpContext.Request.Form.Files;

            string webRootPath = _hostingEnvironment.WebRootPath;// 获取目录下的...Api/wwwroot路径 
            var picturefilePath = FileSaveHelper.Filesave(byThePicture, webRootPath);  // 封装的文件处理方法



            var Picture = new slideshow
            {
                sequence = Int32.Parse(sequence),
                byThePicture = picturefilePath
            };

            _slideshow.Insert(Picture);

            // 返回前端数据
            var res = new
            {
                Code = 1000,
                Data = Picture,
                Msg = "轮播图添加成功"
            };
            return JsonHelper.Serialize(res);

        }

        // 查询轮播图
        [HttpGet,Route("sel_slideshow")]
        public dynamic selSlideshow()
        {
            var Slideshow = _slideshow.Table.Where(x => x.IsDeleted == false).OrderByDescending(x => x.CreatedTime).ToList().FirstOrDefault();

            var res = new
            {
                Code = 1000,
                Data = Slideshow,
                Msg = "查询轮播图成功"
            };
            return JsonHelper.Serialize(res);
        }























    }
}